import type { NextApiResponse } from 'next';
import R from '@common/type/response';
import { deleteFile, fileExists } from '@common/utils/file';
import { AuthenticatedRequest, withAuth } from '@common/middleware/auth';

export async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
  if (req.method !== 'POST') {
    return R.error(res, '只支持POST请求');
  }
  try {
    const { filename } = req.body;
    
    if (!filename) {
      return R.error(res, '缺少文件名参数');
    }
    // 检查文件是否存在
    if (!fileExists(filename)) {
      return R.error(res, '文件不存在');
    }
    // 删除文件
    const success = deleteFile(filename);
    if (success) {
      R.success(res, { message: '文件删除成功' });
    } else {
      R.error(res, '文件删除失败');
    }
  } catch (error: any) {
    console.error('文件清理错误:', error);
    R.error(res, error.message || '文件清理失败');
  }
}

export default withAuth(handler); 